package com.zdd.ehe.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdd.ehe.entity.Permission;
import com.zdd.ehe.entity.RolePermission;
import com.zdd.ehe.entity.UserRole;
import com.zdd.ehe.result.Result;
import com.zdd.ehe.service.IRolePermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZhuiZhu
 * @since 2023-03-13
 */
@Api(tags="角色权限管理模块")
@RestController
@RequestMapping("/rolePermission")
public class RolePermissionController {
    @Resource
    private IRolePermissionService rolePermissionService;

    @ApiOperation("分页获取用户权限接口")
    @PostMapping("/getallrolepermissionbypage")
    public Result getAllUserRoleByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        Page<RolePermission> pagex = new Page<>(page,pageSize);
        Page<RolePermission> pageResult = rolePermissionService.getAllUserRoleByPage(pagex);
        if(pageResult.getSize() == 0){
            return Result.error(4001,"无数据");
        }

        return Result.success("查询成功",pageResult);

    }


    @ApiOperation("通过rolename查询")
    @GetMapping("/getbyrolename")
    public Result getByRoleName(@RequestParam("roleName") String roleName){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("roleName",roleName);
        List<RolePermission> rolePermissionList = rolePermissionService.getAllByRoleName(roleName);
        if(rolePermissionList.size() !=0){
            Map<String,Object> data = new HashMap<>();
            data.put("rolePermissionList",rolePermissionList);
            data.put("total",rolePermissionList.size());
            return Result.success("查询成功！",data);
        }else {
            return Result.error(4001,"未查询到该角色信息");
        }
    }

    @ApiOperation("删除角色信息接口")
    @PostMapping("/deleterolepermissionbyid")
    public Result deleteRolePermissionById(@RequestParam("id") int id){
        RolePermission rolePermission = rolePermissionService.getById(id);
        if(rolePermission == null){
            return Result.error(4001,"编号错误");
        }
        rolePermission.setAuthorizeStatus("0");
        boolean result = rolePermissionService.updateById(rolePermission);
        if(result == false){
            return Result.error(4002,"删除失败");
        }
        return Result.success("删除成功");
    }



    @ApiOperation("重启角色权限接口")
    @PostMapping("/restartrolepermissionbyid")
    public Result restartRolePermissionById(@RequestParam("id") int id){
        RolePermission rolePermission = rolePermissionService.getById(id);
        if(rolePermission == null){
            return Result.error(4001,"编号错误");
        }
        rolePermission.setAuthorizeStatus("1");
        boolean result = rolePermissionService.updateById(rolePermission);
        if(result == false){
            return Result.error(4002,"重启失败");
        }
        return Result.success("重启成功");
    }


    @ApiOperation("获取角色权限信息接口")
    @PostMapping("/showrolepermission")
    public RolePermission showRolePermission(@RequestParam("roleId") int roleId){

        RolePermission rolePermission = rolePermissionService.getById(roleId);
        return rolePermission;
    }

    @ApiOperation("修改角色权限信息接口")
    @PostMapping("/updaterolepermissionbyid")
    public Result updateRolePermissionById(@RequestBody Map<String,Object> data){

        Integer roleId = (Integer) data.get("roleId");
        List<Integer> permissionIds = (List<Integer>) data.get("permissionIds");

        int result = rolePermissionService.deleteById(roleId);
        if(result != -1){
            for(int i = 0;i<permissionIds.size();i++){
                if(permissionIds.get(i) != -1){
                    int permission_id = permissionIds.get(i);
                    RolePermission rolePermission = new RolePermission();

                    rolePermission.setRoleId(roleId);
                    rolePermission.setPermissionId(permission_id);
                    rolePermission.setAuthorizeTime(LocalDateTime.now());
                    rolePermission.setAuthorizeStatus("1");

                    int r = rolePermissionService.insert(rolePermission);
                    if(r == -1){
                        return Result.error("修改失败");
                    }
                }
            }
        }
        return Result.success("修改成功");
    }


    @ApiOperation("根据角色id 查询对应的权限列表的接口")
    @PostMapping("/getpermissionbyid")
    public Result getPermissionByid(@RequestParam("id") int roleId){

        List<Integer> permissionIds = rolePermissionService.getPermissionIdByRoleId(roleId);
        if(permissionIds.size() == 0){
            return Result.error(4001,"查询失败");
        }
        return Result.success("查询成功",permissionIds);
    }


    @ApiOperation("添加角色权限")
    @PostMapping("/addrolepermission")
    public Result addRolePermission(@RequestBody RolePermission rolePermission){
        int r = rolePermissionService.insert(rolePermission);
        if(r != 0){
            return Result.success("添加成功");
        }else {
            return Result.error(4001,"添加失败");
        }
    }


}
